package com.xiongjie.msg;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.DeliveryOptions;

public class MessageDriverProducerVerticle extends AbstractVerticle {

    //以消息总线的形式发送消息，即eventbus。以http的形式处理eventbus的消息传递。
    @Override
    public void start() throws Exception {
        vertx.createHttpServer().requestHandler(req -> {
            //消息头的使用，自动被转发过去
            DeliveryOptions deliveryOptions = new DeliveryOptions();
            deliveryOptions.addHeader("action", "world_action");

            vertx.eventBus().request("hello", "hexingjie", deliveryOptions, message -> {
                if (message.succeeded()) {
                    System.out.println("这里是生产者："+this.toString());
                    System.out.println("接收到的消费者的回覆是: " + message.result().body());

                    //这里是异步的，必须结束响应体
                    req.response().end("Test event bus success: " + message.result().body());
                } else {
                    message.cause().printStackTrace();
                    req.response().setStatusCode(500).end(message.toString());
                }
            });
        }).listen(8886);
    }

}
